using System.Diagnostics;

namespace f1_ans
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] drives = Directory.GetLogicalDrives();
            foreach (string drive in drives)
            {
                TreeNode node = new TreeNode(drive);
                node.Tag = drive;
                node.Nodes.Add("");
                treeView1.Nodes.Add(node);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string path = e.Node.Tag.ToString();
            listView1.Items.Clear();
            ListViewItem.ListViewSubItem[] subItems;
            ListViewItem item = null;

            DirectoryInfo dir = new DirectoryInfo(path);
            foreach (DirectoryInfo subdir in dir.GetDirectories())
            {
                item = new ListViewItem(subdir.Name, 0);
                item.Tag = subdir;
                subItems = new ListViewItem.ListViewSubItem[]
                    {new ListViewItem.ListViewSubItem(item, "Directory"),
                     new ListViewItem.ListViewSubItem(item, subdir.LastAccessTime.ToShortDateString())};
                item.SubItems.AddRange(subItems);
                listView1.Items.Add(item);
            }

            foreach (FileInfo file in dir.GetFiles())
            {
                item = new ListViewItem(file.Name, 1);
                item.Tag = file;
                subItems = new ListViewItem.ListViewSubItem[]
                    { new ListViewItem.ListViewSubItem(item, "File"),
                      new ListViewItem.ListViewSubItem(item, file.LastAccessTime.ToShortDateString())};

                item.SubItems.AddRange(subItems);
                listView1.Items.Add(item);
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;

            string path = listView1.SelectedItems[0].Tag.ToString();

            FileInfo file = new FileInfo(path);

            if (file.Extension.ToLower() == ".txt")
            {
                ProcessStartInfo processStartInfo = new ProcessStartInfo(file.FullName);
                Process process = new Process();
                process.StartInfo = processStartInfo;
                process.StartInfo.UseShellExecute = true;
                process.Start();
            }
        }

        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            TreeNode node = e.Node;
            if (node.Nodes.Count == 1 && node.Nodes[0].Text == "")
            {
                node.Nodes.Clear();
                string path = node.Tag.ToString();
                DirectoryInfo[] subDirs;
                try
                {
                    subDirs = new DirectoryInfo(path).GetDirectories();
                }
                catch
                {
                    return;
                }

                foreach (DirectoryInfo dir in subDirs)
                {
                    TreeNode subNode = new TreeNode(dir.Name);
                    subNode.Tag = dir.FullName;
                    subNode.Nodes.Add("");
                    node.Nodes.Add(subNode);
                }
            }
        }

        private void ListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.List;
        }

        private void TitleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.Tile;
        }

        private void SmallIconToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            listView1.View = View.SmallIcon;
        }
    }
}